package com.chuangke.system.data.builder;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chuangke.common.data.builder.DataBuildConfig;
import com.chuangke.common.data.builder.IDataBuilder;
import com.chuangke.common.utils.SpringContextUtil;
import com.chuangke.system.entity.SysMeasureGroup;
import com.chuangke.system.service.SysMeasureGroupService;

import cn.hutool.core.util.StrUtil;

public class MeasureGroupBuilder implements IDataBuilder<SysMeasureGroup> {

	private static final Logger logger = LoggerFactory.getLogger(MeasureGroupBuilder.class);

	public static final String NAME = "sys_measuregroup";

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public Map<String, SysMeasureGroup> getMetaData() {
		SysMeasureGroupService sysMeasureGroupService = SpringContextUtil.getBean(SysMeasureGroupService.class);
		return sysMeasureGroupService.findAll();
	}

	@Override
	public void build(Object obj, Map<String, SysMeasureGroup> metaData, List<DataBuildConfig> dataBuildConfigList) {
		if(metaData == null) return ;
		try {
			for (DataBuildConfig dataBuildBean : dataBuildConfigList) {
				String id = BeanUtils.getProperty(obj, dataBuildBean.getIdColumn());
				if(StrUtil.isBlank(id)) {
					continue ;
				}
				SysMeasureGroup group = metaData.get(id);
				String value = group == null ? "" : group.getName();
				BeanUtils.setProperty(obj, dataBuildBean.getNameColumn(), value);
			}
		} catch (Exception e) {
			logger.error("设置字段信息出错", e);
		}
	}

}